package com.lrm.web.admin;

import com.lrm.biz.IBlogBiz;
import com.lrm.biz.ITagBiz;
import com.lrm.pojo.Blog;
import com.lrm.pojo.User;
import com.lrm.util.Result;
import com.lrm.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/1.
 * @Modified By:
 */
@RestController
@RequestMapping("admin")
public class BlogController {

    @Autowired
    private IBlogBiz blogBiz;
    @Autowired
    private ITagBiz tagBiz;

    @GetMapping("listBlog")
    public Result listBlog(@PageableDefault(size = 3,sort = {"updataTime"},direction = Sort.Direction.DESC) Pageable pageable,
                           String title,Long typeid,boolean recommend){
        Page<Blog> blogs = blogBiz.listBlog(pageable, title,typeid,recommend);
        return ResultUtil.success(blogs);
    }

    @GetMapping("delBlog/{id}")
    public Result delBlog(@PathVariable Long id){
        Boolean blog = blogBiz.delBlog(id);
        if (blog) {
            return ResultUtil.success();
        }
        return ResultUtil.error(2,"删除失败");
    }

    @PostMapping("addBlog")
    public Result addBlog(Blog blog, HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println("user+++++++++++++"+user);
        blog.setUser(user);
        blog.setTags(tagBiz.listList(blog.getTagIds()));
        Blog b = blogBiz.saveBlog(blog);
        if (b!=null) {
            return ResultUtil.success(b);
        }
        return ResultUtil.error(2,"增加失败");
    }

    @GetMapping("/getBlog/{id}")
    public Result getBlog(@PathVariable Long id){
        Blog blog = blogBiz.getOne(id);
        return ResultUtil.success(blog);
    }
}
